package com.wing.cabinet.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.entity.CellInventoryInout;
import com.wing.cabinet.model.entity.StorageInventorySpace;
import com.wing.cabinet.model.response.CabinetCargoSpaceVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.CabinetCargoSpace;
import com.wing.cabinet.model.request.CabinetCargoSpaceForm;
import com.wing.cabinet.service.CabinetCargoSpaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-收刀柜货位管理", tags = "#/cabinet/cabinetCargoSpace")
@RestController("cabinetCargoSpaceController")
@RequestMapping("cabinet/admin/cabinetCargoSpace")
@RequiredArgsConstructor
public class CabinetCargoSpaceController {

    private final CabinetCargoSpaceService cabinetCargoSpaceService;


    @ApiOperation(value = "查询所有收刀柜货位", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CabinetCargoSpaceVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String cabCode
            , Long cabinetId
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
            ,String mainCabCode
            ,String spaceCode
            ,String spaceCode4Query
            ,String cabCode4Query
            ){

        Page<CabinetCargoSpace> pg = new Page<>(page, limit);

        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("cabCode",cabCode);
        queryParam.put("cabinetId",cabinetId);
        queryParam.put("queryFlag",queryFlag);
        queryParam.put("mainCabCode",mainCabCode);
        queryParam.put("spaceCode",spaceCode);
        queryParam.put("cabCode4Query",cabCode4Query);
        queryParam.put("spaceCode4Query",spaceCode4Query);
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }

        IPage<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "查询所有收刀柜货位", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4MainCab")
    public PageResult<CabinetCargoSpaceVO> list4MainCab(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String cabCode
            , Long cabinetId
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
            ,String mainCabCode
            ,String spaceCode
            ,String spaceCode4Query
            ,String cabCode4Query
    ){

        Page<CabinetCargoSpace> pg = new Page<>(page, limit);

        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("cabCode",cabCode);
        queryParam.put("cabinetId",cabinetId);
        queryParam.put("queryFlag",queryFlag);
        queryParam.put("mainCabCode",mainCabCode);
        queryParam.put("spaceCode",spaceCode);
        queryParam.put("cabCode4Query",cabCode4Query);
        queryParam.put("spaceCode4Query",spaceCode4Query);
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }
        IPage<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4MainCab(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有收刀柜货位", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4pc")
    public PageResult<CabinetCargoSpaceVO> list4pc(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String cabCode
            , Long cabinetId
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
            ,String mainCabCode
            ,String cabName
            ,CabinetCargoSpace.BusinessStatus businessStatus
            ,CabinetCargoSpace.SpaceNature spaceNature
    ){

        Page<CabinetCargoSpace> pg = new Page<>(page, limit);

        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("cabCode",cabCode);
        queryParam.put("cabName",cabName);
        queryParam.put("cabinetId",cabinetId);
        queryParam.put("queryFlag",queryFlag);
        queryParam.put("mainCabCode",mainCabCode);
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }
        if(businessStatus != null){
            queryParam.put("businessStatus",businessStatus.getValue());
        }if(spaceNature != null){
            queryParam.put("spaceNature",spaceNature.getValue());
        }
        IPage<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.pageList4pc(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "柜子库位列表（柜子面板,格子柜上新的格子）", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/cellListLoad")
    public PageResult<CabinetCargoSpaceVO> list4Load(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            , @RequestParam(name = "limit",defaultValue = "10") Integer limit
            , @RequestParam(name = "sort",defaultValue = "cast(t.code as unsigned)") String sort
            , @RequestParam(name = "order",defaultValue = "asc") String order
            , String cabCode
            , Long cabId
            , StorageInventorySpace.BusinessType businessType
            , String skuCode
            ,Integer quantityPackUnit
            ,String queryFlag
    ) {
        Page<CabinetCargoSpaceVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("cabinetId",cabId);
        params.put("cabCode",cabCode);
        if (businessType!=null){
            params.put("businessType",businessType.getValue());
        }
        params.put("skuCode",skuCode);
        params.put("quantityPackUnit",quantityPackUnit);
        params.put("queryFlag",queryFlag);
        Page<CabinetCargoSpaceVO> list = cabinetCargoSpaceService.cellListLoad(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加收刀柜货位", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(CabinetCargoSpaceForm.Add.class) CabinetCargoSpaceForm cabinetCargoSpaceForm) {
        if (cabinetCargoSpaceService.add(cabinetCargoSpaceForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改收刀柜货位", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(CabinetCargoSpaceForm.Update.class) CabinetCargoSpaceForm cabinetCargoSpaceForm) {
        CabinetCargoSpace cabinetCargoSpace = cabinetCargoSpaceService.getById(cabinetCargoSpaceForm.getId());
        if(cabinetCargoSpace == null){
        return JsonResult.fail("修改失败！");
        }
//        CopyOptions copyOptions = CopyOptions.create()
//        .setEditable(CabinetCargoSpace.class)
//        .setIgnoreError(true)
//        .setIgnoreNullValue(true);
//        BeanUtil.copyProperties(cabinetCargoSpaceForm, cabinetCargoSpace, copyOptions);
        cabinetCargoSpace.setSpaceType(cabinetCargoSpaceForm.getSpaceType());
        cabinetCargoSpace.setSpaceProductType(cabinetCargoSpaceForm.getSpaceProductType());
        cabinetCargoSpace.setSkuCode(cabinetCargoSpaceForm.getSkuCode());
        cabinetCargoSpace.setPrincipal(cabinetCargoSpaceForm.getPrincipal());
        cabinetCargoSpace.setPrincipalName(cabinetCargoSpaceForm.getPrincipalName());
        cabinetCargoSpace.setBusinessStatus(cabinetCargoSpaceForm.getBusinessStatus());
        cabinetCargoSpace.setQuantity(cabinetCargoSpaceForm.getQuantity());
        cabinetCargoSpace.setRemark(cabinetCargoSpaceForm.getRemark());
        if (StringUtils.isNotBlank(cabinetCargoSpaceForm.getPrincipal())){
            cabinetCargoSpace.setPrincipal(","+cabinetCargoSpaceForm.getPrincipal()+",");
        }
        if (cabinetCargoSpaceService.updateById(cabinetCargoSpace)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除收刀柜货位", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cabinetCargoSpaceId", value = "收刀柜货位id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long cabinetCargoSpaceId) {
        if (cabinetCargoSpaceService.removeById(cabinetCargoSpaceId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
